package ctr_layer;

import java.util.List;

import model_layer.Customer;
import db_layer.CustomerDB;

public class CustomerCtr
{
	private CustomerDB cust_db;
	public CustomerCtr()
	{
		cust_db = new CustomerDB();
	}
	
	public Customer find_customer_by_id(String id)
	{
		return cust_db.find_customer("id", Integer.parseInt(id));
	}
	
	public Customer find_customer_by_name(String name)
	{
		if(name == null || name.equals(""))
		{
			return null;
		}
		return cust_db.find_customer("name", name);
	}
	
	public List<Customer> find_all_customers()
	{
		return cust_db.find_all_customers();
	}
	
	public List<Customer> find_deleted_customers()
	{
		return cust_db.find_particular_customers("deleted", 1);
	}
	
	public List<Customer> find_non_deleted_customers()
	{
		return cust_db.find_particular_customers("deleted", 0);
	}
	
	public boolean insert_customer(String name, String phone_nr, String email, String address, String zipcode, String city, float discount)
	{
		Customer cust = new Customer();
		cust.setAddress(address);
		cust.setCity(city);
		cust.setDeleted(false);
		cust.setDiscount(discount);
		cust.setEmail(email);
		cust.setName(name);
		cust.setPhone_nr(phone_nr);
		cust.setZipcode(zipcode);
		return cust_db.insert_customer(cust);
	}
	
	public boolean update_customer(Customer cust)
	{
		return cust_db.update_customer(cust);
	}
	
	public boolean delete_customer(int id)
	{
		Customer cust = cust_db.find_customer("id", id);
		cust.setDeleted(true);
		return cust_db.update_customer(cust);
	}
	
	public boolean restore_customer(int id)
	{
		Customer cust = cust_db.find_customer("id", id);
		cust.setDeleted(false);
		return cust_db.update_customer(cust);
	}
	
	public String find_city_by_zipcode(String zipcode)
	{
		return cust_db.find_city(zipcode);
	}
	
	public boolean name_exists(String name)
	{
		return cust_db.find_customer("name", name) != null;
	}
	
	

}
